package com.nttuan.util;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;

import com.nttuan.mycalender.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by NT Tuan on 10/18/2014.
 */
public class Support {
    public static final String TAG = "Support";


    public static String toStringDateTimeMili(Date dateTime){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return simpleDateFormat.format(dateTime);

    }
    public static Date toDateTimeMili(String sDateTime){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        try{
            Date date = simpleDateFormat.parse(sDateTime);
            return date;
        }catch (Exception e){
            Log.d(TAG, e.getMessage());
        }
        return null;

    }
    public static String currentDate(){
         Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_MONTH) + "/" +
                (calendar.get(Calendar.MONTH)+1) + "/" + calendar.get(Calendar.YEAR);
    }
    public static int dateOfWeek(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK) -1;
    }
    public static String currentDayOfWeek(Context context){
        Calendar calendar = Calendar.getInstance();
        String[] arrDate = context.getResources().getStringArray(R.array.dayOfWeek);
        return arrDate[calendar.get(Calendar.DAY_OF_WEEK) - 1];
    }
    public static boolean toActive(String active){
        if(active != null && active.equals("1"))
            return  true;
        return false;
    }
    public static String displayDate(Calendar calendar){
        return calendar.get(Calendar.DAY_OF_MONTH) + "/" +
                (calendar.get(Calendar.MONTH)+1) + "/" + calendar.get(Calendar.YEAR);
    }
    public static String displayDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return  displayDate(calendar);
    }

    public static String displayMinute(int minute){
        int temp = minute % 60;
        if(temp == 0)
            return "00";
        if(temp < 10)
            return  "0"+temp;
        return temp+"";
    }
    public static String displayHour(int minute){
        int temp = minute / 60;
        if(temp == 0)
            return "00";
        if(temp < 10)
            return  "0"+temp;
        return temp+"";
    }
    public static String displayTimeLeft(Context context,int minute){
        if(minute < 60)
            return context.getResources().getString(R.string.minute_short, minute+"");
        if(minute == 60)
            return  context.getResources().getString(R.string.hour_short,"1");
        int m = minute % 60;
        if(m == 0)
            return  context.getResources().getString(R.string.hour_short, minute/60);
        return context.getResources().getString(R.string.time_left, (minute/60)+"", m+"");
    }
    public static String displayTime(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        return simpleDateFormat.format(date);
    }

    public static String toString(Date dateTime){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(dateTime);
    }
    public static String displayMinuteShort(Context context,int minute){
        Resources res = context.getResources();
        if(minute < 60) {
            return  res.getString(R.string.text_minute_short, minute);
        }
        if(minute == 1439)
            return  res.getString(R.string.text_hour, "24");
        return res.getString(R.string.text_date_short, minute/(24*60)+"");

    }
    public static String displayMinute(Context context,int minute){
        Resources res = context.getResources();
        if(minute < 60) {
            return minute +" "+ res.getString(R.string.text_minute);
        }
        if(minute == 1439)
            return "24 " + res.getString(R.string.text_hour);
        int temp = minute % 60;
        return minute / 60 +" "+ res.getString(R.string.text_hour) +
                (temp == 0 ? "":" "+temp+" "+ res.getString(R.string.text_minute));

    }
    public static Date subDate(Date date, int minute){
        Date result = new Date();
        result.setTime(date.getTime() - minute*60000);
        return Support.removeMilliSecond(result);
    }
    public static Date sysDate(Date res, Date target) {
        Date temp = new Date();
        temp.setDate(res.getDate());
        temp.setMonth(res.getMonth());
        temp.setYear(res.getYear());
        temp.setHours(target.getHours());
        temp.setMinutes(target.getMinutes());
        return temp;
    }
    public static boolean equalDate(Date date1, Date date2){
        return (date1.getDate() == date2.getDate() && date1.getMonth() == date2.getMonth()
                && date1.getYear() == date2.getYear());
    }
    public static Date removeMilliSecond(Date date){
        return  toDateTime(Support.toString(date),false);
    }
    public static Date toShortDate(String sDate){
        SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("dd/MM/yyyy");
        try{
            Date date = simpleDateFormat.parse(sDate);
            return date;
        }catch (Exception e){
            Log.d(TAG, e.getMessage());
        }
        return null;
    }
    public static Date toLongDateTime(String sDate){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Date date = simpleDateFormat.parse(sDate);
            return date;
        }catch (Exception e){
            Log.d(TAG, e.getMessage());
        }
        return null;
    }
    public static Date toDateTime(String sDate, boolean fromView){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if(fromView)
            simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        try{
            Date date = simpleDateFormat.parse(sDate);
            return date;
        }catch (Exception e){
            Log.d(TAG, e.getMessage());
            return null;
        }

    }
    public static String displayRepeat(Context context, String repeat){
        Resources rs = context.getResources();
        String[] arrRepeat = repeat.split(",");
        String[] dateOfWeek = rs.getStringArray(R.array.dayOfWeekShort);
        if(arrRepeat.length == 7)
            return rs.getString(R.string.every_date);
        String result ="";
        int pos = 0;
        for(int i =0; i< arrRepeat.length;i++){
            pos = Integer.parseInt(arrRepeat[i]);
            if(i < arrRepeat.length - 1)
                result +=dateOfWeek[pos] +" ,";
            else
                result +=dateOfWeek[pos];
        }
        return  result;

    }



    public static Date nextDate(Date start, int dateOfWeek){
        while (Support.dateOfWeek(start) != dateOfWeek){
            start = Support.addDateTime(start,60*24);
        }
        return start;
    }
    public static int goodNumber(int res, boolean isUpper){
        int distant = 10;
        int proc = res;
        if(isUpper){
           if(proc > 10)
               proc = proc % 10;
            if(proc == 0)
                return res;
            res += (distant - proc);
            return  res;
        }
        if(proc > 10)
            proc = proc % 10;
        if(proc == 0)
            return res;
        return  res - proc;
    }
    public static Date startWeek(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        while (calendar.get(Calendar.DAY_OF_WEEK) > 2)
            calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }
    public static Date endWeek(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        while (calendar.get(Calendar.DAY_OF_WEEK) <7)
            calendar.add(Calendar.DATE, 1);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }
    public static int timeLeft(Date start){
        Calendar calendar = Calendar.getInstance();
        Date temp = calendar.getTime();
        temp.setHours(start.getHours());
        temp.setMinutes(start.getMinutes());
        return  subTime(calendar.getTime(), temp);
    }
    public static int timeLeft2(Date statAt){
        Calendar calendar = Calendar.getInstance();
        Date temp = calendar.getTime();
        temp = Support.removeMilliSecond(temp);
        statAt = Support.removeMilliSecond(statAt);
        temp.setDate(statAt.getDate());
        temp.setMonth(statAt.getMonth());
        temp.setYear(statAt.getYear());
        long milliseconds = temp.getTime() - statAt.getTime();
        return (int)milliseconds/ (1000*60);
    }
    public static int subTime(Date start, Date end){
           long milliseconds = end.getTime() - start.getTime();
        return (int)milliseconds/ (1000*60);
    }

    public static Date addDateTime(Date date, int minute){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }
    public static Date startOfDate(Date date){
        Date temp = new Date();
        temp.setYear(date.getYear());
        temp.setDate(date.getDate());
        temp.setMonth(date.getMonth());
        temp.setHours(00);
        temp.setMinutes(00);
        return temp;
    }
    public static Date endOfDate(Date date){
        Date temp = new Date();
        temp.setYear(date.getYear());
        temp.setDate(date.getDate());
        temp.setMonth(date.getMonth());
        temp.setHours(23);
        temp.setMinutes(59);
        return temp;
    }
    public static boolean validateTime(Date start, Date end){
        if(start.getHours() > end.getHours())
            return false;
        if(start.getHours() == end.getHours())
            if(start.getMinutes() > end.getMinutes()-1)
                return false;
            else
                return true;
        return true;
    }

//    other
    public static int toPixel(int dp, Context context){
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return (int)px;
    }
    public static boolean toBoolean(int i){
        if(i == 1)
            return true;
        return false;
    }
}
